package cn.zh.org.controller;

import cn.zh.base.util.AjaxResult;
import cn.zh.base.util.PageList;
import cn.zh.org.domain.Employee;
import cn.zh.org.query.DepartmentQuery;
import cn.zh.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/emp")
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService service;

    @GetMapping
    public List<Employee> findAll() {
        return service.findAll();
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        if (null == employee.getId()) {
            service.add(employee);
        } else {
            service.update(employee);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        service.delete(id);
        return AjaxResult.me().success();
    }

    @PatchMapping("/batchdel")
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        service.batchDel(ids);
        return AjaxResult.me().success();
    }

    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody DepartmentQuery query) {
        return service.queryByPage(query);
    }

    @GetMapping("/findEmpByDeptId/{id}")
    public List<Employee> findEmpByDeptId(@PathVariable Long id) {
        List<Employee> list = service.findEmpByDeptId(id);
        return list;
    }

}
